using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Modules.Abstracts;
using FiltronASP.Models.DAO.Interfaces;
using System.Web.UI.WebControls;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Helpers.CatalogueMenu;
using FiltronASP.Helpers.Translations;

namespace FiltronASP.Modules
{
    public class lastSearchesFullList : AbstractModule
    {
        private Page currentPage;

        /// <summary>
        /// Klasa odpowiedzialna za generowanie contentu przez modul.
        /// </summary>
        /// <param name="page">biezaca strona</param>
        /// <returns>content</returns>
        public override string GenerateContent(FiltronASP.Models.Page page)
        {
            this.currentPage = page;
            int languageId = Convert.ToInt32(page.language_id);
            int pageId = page.page_id;
            return this.generateInnerContent(page);
        }

        private string generateInnerContent(Page page) {
            string htmlToReturn = "";
            htmlToReturn += " <div id=\"contentHolderInner\">";
            htmlToReturn += "<div id=\"lastSearchesListContainer\">";
            htmlToReturn += String.Format("<h3 class=\"booked\">{0}</h3>", Helpers.Translations.translationHelper.getTranslation("_lastSearches", Convert.ToInt32(page.language_id)));
            ILastSearchesDAO lastSearchesDAO = new LastSearchesDAO();
            IQueryable<LastSearch> paginatedLSCollection = lastSearchesDAO.getLastSearchesPaginated(Convert.ToInt32(page.language_id), 0);
            if (paginatedLSCollection.Any())
            {
                List<LastSearch> lastSearchList = new List<LastSearch>();
                lastSearchList = paginatedLSCollection.ToList();
                for (int i = 0; i < 100; )
                {
                    htmlToReturn += "<ul style=\"float: left;\">";
                    List<LastSearch> lastSearchListInLoop = lastSearchList.Skip(i).Take(20).ToList();
                    foreach (LastSearch singleLoop in lastSearchListInLoop)
                    {
                        htmlToReturn += String.Format("<li><a href=\"{2}{1}\">{0}</a></li>",
                        singleLoop.last_search_name,
                        singleLoop.last_search_link,
                        Helpers.Translations.translationHelper.getTranslation("_lastSearchesLink", Convert.ToInt32(page.language_id)));
                    }
                    htmlToReturn += "</ul>";
                    i += 20;
                }

                IQueryable<LastSearch> paginatedLSCollectionNext = lastSearchesDAO.getLastSearchesPaginated(Convert.ToInt32(page.language_id), 1);
                List<LastSearch> lastSearchListNext = new List<LastSearch>();
                lastSearchListNext = paginatedLSCollectionNext.ToList();

                if (lastSearchListNext.Count == 100)
                {
                    htmlToReturn += "<div onclick=\"lastSearchesList(1, " + Convert.ToInt32(page.language_id) + ");\"class=\"lastSearchListRight\">&raquo;</div>";
                }

            }




            htmlToReturn += "</div>";
            
            return htmlToReturn;
        }
    }
}
